package popcorn.ticket.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.dao.UsersMapper;
import popcorn.ticket.entity.Users;
import popcorn.ticket.service.UsersService;
import popcorn.ticket.tool.AjaxResponse;

@RestController
public class RegisterController {
	@Autowired
	UsersService usersService;
	@Autowired
	UsersMapper usersmapper;

	Logger logger = Logger.getLogger(RegisterController.class);
	
	// 注册方法
	@PostMapping(value = "reg", consumes = "application/json", produces = "application/json;charset=utf-8")
	public AjaxResponse reg(@RequestBody Users users, HttpSession session) {
		// 初始化用户权限
		users.setUsercondition(1);
		users.setUserpower(1);
		String msg = "注册失败";
		// 调用业务层，添加用户信息
		int res = usersService.addUsers(users);

		if (res == 2) {
			Users list1 = usersmapper.selectByName(users.getUsername());
			// 保存用户id到session
			session.setAttribute("userid", list1.getUserid());
			msg = "注册成功";
		}
		AjaxResponse aj = new AjaxResponse(msg, res);
		return aj;

	}

	// 前台登录方法
	@PostMapping(value = "loginForward", produces = "application/json;charset=utf-8")
	public AjaxResponse loginForward(String username,String userpwd, String cksremember, HttpServletResponse response, HttpSession session) {
		// 获得用户名，用户密码，检查用户是否勾选记住密码
		String msg = "请检查用户名/手机号/密码是否正确";
		logger.info("user:"+username+","+userpwd);
		logger.info("是否存:"+cksremember);
		boolean flag2 = cksremember.equals("ok");
		// 调用service中的验证身份方法
		boolean flag = usersService.login(username,userpwd,session);
		
		if (flag) {
			
			if (flag2) {
				Cookie cookie1 = new Cookie("username", username);
				Cookie cookie2 = new Cookie("userpwd", userpwd);
				// 保存时间为7天
				cookie1.setMaxAge(7 * 24 * 60 * 60);
				cookie2.setMaxAge(7 * 24 * 60 * 60);
				// 在webapp文件夹下的所有应用共享cookie
				cookie1.setPath("/");
				cookie2.setPath("/");
				response.addCookie(cookie1);
				response.addCookie(cookie2);
			}
			// 转发
			msg = "登录成功";
			AjaxResponse aj = new AjaxResponse(msg, 200);

			return aj;
		}
		// 传参
		AjaxResponse aj = new AjaxResponse(msg, 200);

		return aj;
	}
	
	// 退出登录，注销session
	@RequestMapping(value = "logout")
	public AjaxResponse Logout(HttpServletRequest request, HttpServletResponse response) {
		
		request.getSession().removeAttribute("userid");
		request.getSession().removeAttribute("username");
		request.getSession().removeAttribute("userpic");
		AjaxResponse aj = new AjaxResponse("退出成功", 200);
		return aj;
	}

	// 拦截器设置
	@RequestMapping(value = "tishi")
	public AjaxResponse lanjie() {
		AjaxResponse aj = new AjaxResponse("请先登录再操作！", 200);
		return aj;
	}
	
	//选票时访问的页面
	@RequestMapping(value = "xuanpiao")
	public AjaxResponse xuanpiao() {
		AjaxResponse aj = new AjaxResponse("我是选票！", 200);
		return aj;
	}
	
	//收藏时访问的页面
	@RequestMapping(value = "xiankan")
	public AjaxResponse xiankan() {
		AjaxResponse aj = new AjaxResponse("我是想看！", 200);
		return aj;
	}
}
